package com.djz.tool;


import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

@SpringBootApplication
@EnableConfigurationProperties
public class ToolApplication {
    public static void main(String[] args) {
        SpringApplication.run(ToolApplication.class, args);
    }
    @Bean
    public CommandLineRunner testDatabaseConnection(DataSource dataSource) {
        return args -> {
            try (Connection conn = dataSource.getConnection()) {
                DatabaseMetaData meta = conn.getMetaData();
                System.out.println("✅ 数据库连接成功！数据库版本：" + meta.getDatabaseProductVersion());
            } catch (SQLException e) {
                String errorMsg = String.format(
"               ❌ 数据库连接失败！"+
"              检查项："+
"              1. MySQL 服务是否启动"+
"              2. 用户名/密码是否正确"+
"              3. 数据库名是否正确"+
"              4. 防火墙是否开放端口" +
"              错误详情：%s", e.getMessage());
                throw new IllegalStateException(errorMsg, e);
            }
        };
    }
}
